﻿
using Microsoft.EntityFrameworkCore;
using OpenAuth.App.Contract.Request;
using OpenAuth.App.Interface;
using OpenAuth.App.Response;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository.Domain.Contract;
using OpenAuth.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OpenAuth.App.Contract
{
   public class CarryApp : BaseStringApp<Currency, OpenAuthDBContext>
    {
        /// <summary>
        /// 注入基类
        /// </summary>
        private RevelanceManagerApp _repository;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="unitWork"></param>
        /// <param name="repository"></param>
        /// <param name="app"></param>
        /// <param name="auth"></param>
        public CarryApp(IUnitWork<OpenAuthDBContext> unitWork,IRepository<Currency, OpenAuthDBContext> repository,RevelanceManagerApp app,IAuth auth) : base(unitWork, repository, auth)
        {
            _repository = app;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="w"></param>
        public void Add(Currency w)
        {
            Repository.Add(w);
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public async Task<TableData> Load(CurrencyDto request)
        {
            var result = new TableData();
            var objs = UnitWork.Find<Currency>(null);
            result.data = await objs.OrderBy(u => u.Id)
                .Skip((request.page - 1) * request.limit)
                .Take(request.limit).ToListAsync();
            result.count = await objs.CountAsync();
            return result;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="name"></param>
        public void Delete(string name)
        {
            //根据名字湖区当前数据
            var lint = UnitWork.Find<Currency>(null).Where(x => x.ContractNo == name).SingleOrDefault();
            //删除该数据
            Repository.Delete(lint);
            //提交
            Repository.Save();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="w"></param>
        public void Upt(CurrencyAddDto w)
        {
            var user = _auth.GetCurrentUser().User;
            UnitWork.Update<Currency>(u => u.Id == w.Id, u => new Currency
            {
                Id=w.Id,
                ContractNo = w.ContractNo,
                ContractTitle = w.ContractTitle,
                OppositeUnit = w.OppositeUnit,
                OppositePerson = w.OppositePerson,
                ContractCategory = w.ContractCategory,
                SigningDate = w.SigningDate,
                ContractAmount = w.ContractAmount,
                ProjectDescription = w.ProjectDescription,
                ChangeClause = w.ChangeClause,
                ContractText = w.ContractText,
                Handler = w.Handler,
                CreationTime = w.CreationTime,
                Status = w.Status,
                Approver = w.Approver,
            });

        }
    }
}
